Skip to content

feat(agent): lk agent create and deploy from source through the Public API when signed in - #1141

Open
Topherhindman wants to merge 12 commits into
topherhindman/agent-user-auth-writesfrom
topherhindman/agent-user-auth-deploy
Open

Topherhindman wants to merge 12 commits into
topherhindman/agent-user-auth-writesfrom
topherhindman/agent-user-auth-deploy

Conversation

@Topherhindman

@Topherhindman Topherhindman commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Under --user-auth, lk agent create, lk agent deploy and lk agent init --deploy (from source) now go through the Public API's AgentService in place of the token bridge. The bridge needs Agent.Admin, so before this only a project admin could create or deploy an agent. Now a WRITE member can do both. Secrets still need a project admin, and the refusal says that a writer can deploy without them.

Stacked on #1140.

How a deploy runs

  1. CreateAgent (create only) registers the agent in the region that lk resolved (--region or the picker, as today).
  2. DeployAgent sets the secrets and returns the version's tag and a presigned POST.
  3. lk builds the same tarball as with an API key (cloudagents.CreateSourceTarball with lk's excludes) and uploads it with cloudagents.MultipartUpload.
  4. StartAgentBuild starts the build and returns when the build is queued.
  5. lk polls GetAgent and ListAgentVersions every 5 seconds. It prints each new status of the target deployment. When the version with the tag is Available or Failed, or the deployment is Build Failed, it prints the version's build log from GetAgentBuildLog and stops.

Commands after this PR

Command Under --user-auth Who
agent create (source), agent init --deploy CreateAgent, DeployAgent, upload, StartAgentBuild, poll WRITE (ADMIN with secrets)
agent deploy (source) DeployAgent, upload, StartAgentBuild, poll WRITE (ADMIN with secrets)
agent list, status, config, versions, dockerfile, build logs AgentService (#1138, #1139) READ
agent secrets AgentService (#1139) ADMIN
agent restart, rollback, promote AgentService (#1140) WRITE
agent delete, update-secrets, update with secrets AgentService (#1140) ADMIN
agent create --image/--image-tar, deploy --image/--image-tar bridge (registry push) ADMIN, with the bridge's refusal
runtime logs, update-secrets --overwrite bridge ADMIN, with the bridge's refusal
agent private-link * bridge (native in the next PR) ADMIN, with the bridge's refusal

What's in it

  • The client comes from feat(agent): lk agent list, status and config through the Public API when signed in #1138's regeneration (livekit/public-api-server@372fb27e, production v2026-12-02-2258), which also has the private-link routes that the next PR uses. This PR has no regeneration of its own.
  • feat(public): CreateAgent, DeployAgent (returns a SourceDeploy: tag, upload URL, form fields) and StartAgentBuild.
  • feat(agent): native CreateAgent and DeployAgentV2 in userAuthAgentAPI, with the poll and the error mapping.
  • fix(agent): the poll reads the deployment's status in the agent's cloud.agentDeployments, which replaced info in livekit/public-api-server#240.

Decisions

  • Polling. The interval is 5 seconds. lk keeps its 15-minute build timeout, and Ctrl-C works as before: lk says that the build continues on the server, which is true once StartAgentBuild has returned. The poll reads the deployment's row in GetAgent (the status of the target deployment, not of production) and the version in ListAgentVersions. A poll that fails transiently (429, 5xx, no answer) is made again up to 4 times with a doubling backoff from 1 second; when it still fails, or fails another way, lk says that the build continues and to run lk agent status. When the deployment has left Setting Up, Building and Scheduling and the version is still not listed a minute later, lk says that the build ended without the version and points at lk agent versions and the build log.
  • Interrupt before the build. A Ctrl-C or a timeout before StartAgentBuild returns "interrupted before the build started: no version was built or deployed, but the secrets this deploy sent are set, and the agent's status may show Building". lk would otherwise read it as a disconnect and say that the build continues. (With an API key, the SDK has the same gap.)
  • Create keeps the agent on failure. If create registers the agent and then anything fails (upload, build start, build, Ctrl-C, timeout), lk saves the ID in livekit.toml, prints "Created agent with ID [CA_…]", as the --image path does once it registers the agent, and the error says "created agent CA_…, but …". lk agent deploy and status then find the agent. With an API key, cloud agents' CreateAgent registers and builds in one call, so nothing changes there. A rerun of lk agent create still creates another agent in both modes, as before.
  • Secrets. Create sends them on CreateAgent, and deploy sends them on DeployAgent. When a member below admin sends secrets, lk says: "this command needs a project admin. Write access to the project is enough without secrets (from --secrets, --secrets-file, --secret-mount or an env file): a project admin can set them with lk agent update-secrets. …". Invalid secrets get a line each, by name, as in feat(agent): lk agent restart, rollback, promote, delete and secrets through the Public API when signed in #1140.
  • Agent Builder agent. A deploy of one names lk agent builder deploy AB_…, with the AB_ ID from the refusal's ResourceInfo detail. A hint for a writer who needs an admin says a project API key also works: "pass --legacy-auth", as chore(auth): prepare for migration to user-auth as default #1156's hints do.
  • Region. lk always resolves exactly one region before create. Today that is the --region flag, the picker, or a non-interactive error. userAuthAgentAPI.CreateAgent refuses any other count.
  • Runtime logs after create. The "view logs?" prompt after an interactive create still streams through the bridge. lk now fills the response's server regions from GetAgent, so the prompt works for an admin. A writer gets a warning that runtime logs need a project admin, and create exits 0.

Output differences vs the API-key path

  • The live buildkit stream (progress UI, queue lines such as "Waiting for an available builder") is replaced by "Building version T1. This deploy continues even if you disconnect.", a Status: <status> line on each change, and the full build log, as plain text, at the end.
  • A failed build prints its log and then "the build of version T1 failed: its build log is above". With an API key, the error is the server's BUILD ERROR: line.
  • The other lines (Created agent with ID, Build completed …, Completed deployment of agent … to production, links) are the same. The testscript compares them.

Open questions

  • A build can take about 25 minutes, and lk's timeout is 15 minutes in both modes. Should --user-auth wait longer, since it costs only polls? This PR keeps 15 minutes.
  • lk agent create reads .env/.env.local by default, so a writer whose env file has any secret other than LIVEKIT_URL/API_KEY/API_SECRET is refused. They can pick [none] in the picker, or remove the file. Should lk skip the env file under --user-auth for a non-admin, or ask?
  • MultipartUpload takes no context (Upload cloudagents source with the caller's context and HTTP client server-sdk-go#1096 adds one). A Ctrl-C during a large upload waits for the upload to end.
  • If the upload or StartAgentBuild never happens, cloud agents keeps the deployment Building (Agent Hosting question in livekit/public-api-server#244).
  • The version statuses Available and Failed are cloud agents' strings, which the Public API passes through. Should the API give them an enum?

Blockers

  • pkg/public generated from livekit/public-api-server@372fb27e (production v2026-12-02-2258).

Testing

  • GOPRIVATE= go build ./..., go vet ./cmd/lk/... ./pkg/..., go test ./cmd/lk/... ./pkg/... and golangci-lint run ./cmd/lk/... ./pkg/public/... pass.
  • New testscript agent-create-deploy-with-user-auth: create and deploy from a Python project, once with an API key (fake cloud agents, fake presigned upload target, empty build stream) and once under --user-auth (fake Public API, same upload target). Both outputs are compared in full, and so is the request log, which has no token request. Also covers a failed build that prints its log.
  • Unit tests (agent_user_auth_deploy_test.go) against a fake Public API that also serves the presigned POST:
    • success: the order of calls, the form fields, the tarball contents (no livekit.toml, .env* or node_modules), the status lines, the build log, and the server regions;
    • a non-production deployment follows its own status;
    • a failed build, both from the version and from Build Failed;
    • a timeout: polls until the context ends and returns DeadlineExceeded;
    • a cancel before the build starts;
    • the refusals: secrets that need an admin, a reader, invalid secrets, an Agent Builder agent, and too many builds;
    • a lost poll.
  • pkg/public tests for the three calls' requests and bodies.

Review fixes

  • Tests prove what each request sends. fakeBuild records each URL's query and each JSON body; the unit tests assert create's region and secrets, deploy's deployment and secrets, and the build's tag, deployment and attributes. The create/deploy script runs --user-auth with --secrets, --attribute and --deployment staging and prints the bodies with requests -v (secret values redacted). Dropping any of them fails a test (checked by mutation).
  • A build that ends without its version (a newer deploy replaced it, the builder restarted) no longer polls to the 15-minute timeout: see Polling.
  • A transient poll failure no longer ends the deploy: see Polling.
  • Create saves the agent's ID when it fails after registering the agent: see Create keeps the agent on failure.
  • The interrupt-before-build message says what is true: see Interrupt before the build.
  • A refused runtime log after a successful create is a warning, exit 0: see Runtime logs after create.
  • Rebased on feat(agent): lk agent restart, rollback, promote, delete and secrets through the Public API when signed in #1140's LIVEKIT_ change. TestUserAuthDeployAgentRefusals now renders a secret name that the Public API still refuses (beacac2): it no longer refuses a LIVEKIT_ name (livekit/public-api-server#242). go build, go vet, go test ./cmd/lk/... ./pkg/... and golangci-lint pass.
  • macOS: two tests gave the deploy a 50 ms deadline, which could expire before StartAgentBuild on a slow runner. They now end the deadline after a set number of polls; they pass 10 times under -race, and test (macos-latest) passes.
  • Still open: a 15-minute timeout under --user-auth still says "build timed out possibly due to large image size", as with an API key, though the build continues on LiveKit Cloud.

Demo

Recorded on staging, not uploaded yet: ~/hosted-agents-work/demos/M4/m4-deploy-from-source.mp4 (write-up ~/hosted-agents-work/demos/M4/README.md). Local Public API from this stack against staging cloud-agents (staging database read-only); lk from #1142. READ is refused a create; WRITE creates an agent from source and deploys a new version (real builds, about two minutes each); WRITE with a secret in .env.local is refused.

Depends on #1136 (the hints name lk agent builder).

CreateAgent registers an agent in a region, DeployAgent returns the tag
of the version and the presigned POST to upload the source to, and
StartAgentBuild starts the build of what was uploaded. The build runs on
after StartAgentBuild returns; callers poll GetAgent and
ListAgentVersions for its end.
Under --user-auth, lk agent create, lk agent deploy and lk agent init
--deploy no longer go through the token bridge, which needs a project
admin. CreateAgent registers the agent in its region, DeployAgent returns
the tag of the version and where to upload the source, lk uploads the
same tarball it uploads with an API key, and StartAgentBuild starts the
build. A WRITE member may do all of it; secrets still need a project
admin, and the refusal says that a writer may deploy without them.

The Public API doesn't stream the build, so lk polls the agent and its
versions every 5 seconds, prints each new status of the deployment, and
prints the version's build log once the build ends. lk's build timeout
and Ctrl-C keep their meaning: the build continues on LiveKit Cloud. An
interrupt before the build starts says that nothing was deployed.
fakeBuild recorded only each request's method and path, so the tests
passed with create's secrets or region, deploy's deployment or secrets,
or the build's deployment or attributes dropped. It now records each
URL's query and each JSON body, and the tests assert them. The
create/deploy script passes secrets, attributes and a deployment under
--user-auth and prints the bodies with `requests -v`.
… behind

"nothing was deployed" wasn't so: before the build starts, DeployAgent
has already set the secrets the deploy sent, and cloud agents shows the
production deployment as Building. The message now says that no version
was built or deployed, that the secrets are set, and that the status may
show Building.
…siently

One failed read of the agent or its versions, such as a 503, a network
error or a rate limit, ended the deploy with "can't follow the build".
awaitBuild now makes such a read again, up to pollAttempts times with a
doubling backoff, before it gives up. A 401, a 403 or any other 4xx
other than 429 fails at once, as before.
…out its version

awaitBuild stopped only when the version was Available or Failed, or the
deployment showed Build Failed. A build that ended without listing its
version, such as one a newer deploy replaced or one the builder dropped,
was polled until the 15-minute timeout, which then blamed a large image.
Once the deployment has left Setting Up, Building and Scheduling, and the
version is still not listed buildEndGrace (a minute) later, awaitBuild
now says that the build ended without the version, and points at
`lk agent versions` and the build log.
…r step fails

Under --user-auth, create registers the agent, then uploads and builds
its source. When anything failed after the agent was registered (the
upload, a build that didn't start or failed, Ctrl-C, the timeout), lk
didn't save livekit.toml, and on a timeout or Ctrl-C its message dropped
the agent's ID. lk now saves the ID and says "Created agent with ID",
as the --image path does once it registers the agent, so that
`lk agent deploy` and `lk agent status` find it; Ctrl-C points at
`lk agent status`. With an API key, cloud agents' CreateAgent registers
and builds in one call, and nothing changes.
… warning

After create succeeded, answering yes to "view logs?" as a member below
admin got the token bridge's refusal and exit status 1, though the agent
was created. lk can't tell the member's role without asking, so it now
warns that runtime logs need a project admin and exits 0. lk agent logs
still fails on the same refusal.
…fuses

The Public API no longer refuses a LIVEKIT_ name
(livekit/public-api-server#242), so TestUserAuthDeployAgentRefusals
renders a name that it refuses: one that starts with a digit.
…'s cloud

public.Agent's Info is Cloud since the Public API moved it
(livekit/public-api-server#240), so awaitBuild reads the deployment's
status in Cloud's deployments and returns Cloud once the build
succeeds. The deploy tests and the create/deploy script answer GetAgent
in the new shape.
… deploy

As for the other writes, a deploy that AgentService refuses because the
agent is an Agent Builder agent now names the lk command to use, with
the Agent Builder agent's ID from the refusal's ResourceInfo detail:
lk agent builder deploy AB_ID, in place of the dashboard.
TestUserAuthDeployAgentTimeout failed on CI's macOS runner: its 50ms
deadline passed before StartAgentBuild, so the deploy stopped as one
interrupted before its build started, which doesn't wrap the deadline's
error. The last case of TestUserAuthDeployAgentBuildEndsWithoutVersion
had the same race.

Both now use a context that ends as a passed deadline does once the
fake has answered a number of version polls, so the build has always
started, however slow the runner.
@Topherhindman
Topherhindman force-pushed the topherhindman/agent-user-auth-deploy branch from 24a0b1c to f9ad76e Compare December 3, 2026 00:32
@Topherhindman
Topherhindman marked this pull request as ready for review December 3, 2026 00:58

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

2 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

Comment thread cmd/lk/agent_user_auth.go
Comment on lines +456 to +458
d, err := a.client.DeployAgent(ctx, a.projectID, agentID, deployment, secrets)
if err != nil {
return nil, deployError(err, agentID, secrets)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Canceled deployment reports false success

If Ctrl-C interrupts DeployAgent before upload, the CLI exits successfully and says the build continues. It never calls StartAgentBuild, so no build runs.

Learn more

The signed-in create and deploy commands first prepare a deployment, then upload source, then explicitly queue the build. If Ctrl-C cancels the preparation request, deployError and agentServiceError preserve the cancellation. Both createAgent and deployAgent interpret that cancellation as a detached build and return success. The explicit interruption check only runs after upload, so it cannot catch this case.

Example: Run lk --user-auth agent deploy and press Ctrl-C during the Public API preparation request. The CLI says the deploy continues, but it has not uploaded the source or asked the server to build it.

Recommended fix: Give a confirmed queued build that the CLI stops following a distinct error type and use it for the signed-in success-on-disconnect path. Keep the registered agent ID on partial create, preserve the legacy client behavior, and test cancellation during preparation separately from cancellation during polling.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant